package cn.javaweb.model;

import cn.javaweb.entity.Course;
import cn.javaweb.entity.User;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CourseModel extends BaseModel{

    public Object parseObject(ResultSet rs){

        Course course = new Course();
        try{
            course.setCourse_id(rs.getInt("course_id"));
            course.setCourse_name(rs.getString("course_name"));
            course.setStart_time(rs.getDate("start_time"));
            course.setEnd_time(rs.getDate("end_time"));
            course.setStatus(rs.getInt("status"));
            return course;
        }catch (Exception e){

        }
        return null;

    }

    public List<Course> getCourseList(User user){

        Date now = new Date();

        String sql = "select course.*, signin.status  from course left join signin on course.course_id=signin.course_id and signin.user_id=? where TO_DAYS(start_time)=TO_DAYS(NOW())";

        Object [] params = {user.getId()};
        List<Object> list = this.selectAll(sql, params);

        List<Course> re = new ArrayList<Course>();

        for(Object one:list){
            re.add((Course)one);
        }
        return re;
    }

    public boolean signin(int course_id, int user_id){

        String sql = "insert into signin (user_id, course_id, signin_time, status) values ( ?, ?, NOW(), 1)";
        Object [] params = {user_id, course_id};

        int r = update(sql, params);

        return true;


    }

    public int getStatistic( String type, int user_id){
        String sql = "";
        Object[] params = {user_id};
        if (type.equals("qq")) {
            sql = "select count(*) cnt from signin where user_id=? and status=0 ";
        }else if (type.equals("all")) {
            sql = "select count(*) cnt from signin where user_id=? and status=1 ";
        }else if (type.equals("cd")) {
            sql = "select count(*) cnt from signin where user_id=? and status=2 ";
        }else if (type.equals("qj")) {
            sql = "select count(*) cnt from signin where user_id=? and status=3 ";
        }else{
            return 0;
        }
        return getCount(sql, params);
    }
}
